<?php
import( 'classes.services.GenericService' );
import( 'de.ceus-media.database.StatementBuilder' );
import( 'classes.models.BugStatements' );
import( 'classes.models.Bug' );
import( 'classes.models.User' );
/**
 *	Main Services.
 *	@package		tools.BugTracker
 *	@extends		GenericService
 *	@uses			StatementBuilder
 *	@uses			BugStatements
 *	@uses			Bug
 *	@uses			User
 */
/**
 *	Main Services.
 *	@package		tools.BugTracker
 *	@extends		GenericService
 *	@uses			StatementBuilder
 *	@uses			BugStatements
 *	@uses			Bug
 *	@uses			User
 */
class Main extends GenericService
{
	/**
	 *	Constructor.
	 *	@access		public
	 *	@return		void
	 */
	public function __construct()
	{
		parent::__construct();
		$config	= $this->ref->get( 'config' );
		$ir		= new IniReader( 'config/rss.ini', true );
		$this->rss	= $ir->toArray();
	}
	
	/**
	 *	Returns Count of all Articles.
	 *	@access		public
	 *	@return		string
	 */
	public function getTotalBugCount()
	{
		$request		= $this->ref->get( "request" );
		$output			= $request->get( 'output' );
		if( !$output )
			return "No Output Format selected (xml,wddx,json,php,txt).";
		if( !in_array( $output, $this->formats ) )
			return "No Output Format '".$output."' available.";

		$bug	= new Bug();
		$count	= $bug->getAllCount();
		if( $output == "xml" )
		{
			$root	=& new XML_DOM_Node( 'bugs' );
			$node	=& new XML_DOM_Node( 'count', $count );
			$root->addChild( $node );
			$xb	= new XML_DOM_Builder();
			header('Content-Type: text/xml');
			return $xb->build( $root );
		}
		else if( $output == "json" )
			return $this->toJSON( $count );
		else if( $output == "php" )
			return $this->toPHP( $count);
		else if( $output == "txt" )
			return $count;
	}

	/**
	 *	Returns Count of all Articles.
	 *	@access		public
	 *	@return		string
	 */
	public function getOpenBugsForUser()
	{
		$dbc		= $this->ref->get( "dbc" );
		$config		= $this->ref->get( "config" );
		$request	= $this->ref->get( "request" );
		$language	= $this->ref->get( 'language' );
		$language->loadLanguage( 'bug' );
		$output		= $request->get( 'output' );
		
		
		$user_id	= $request->get( 'user_id' );
		if( !$output )
			return "No Output Format selected (xml,json,php).";
		if( !in_array( $output, $this->formats ) )
			return "No Output Format '".$output."' available.";
		if( !$user_id )
			return "No User ID given.";

		$conditions	= array(
			'to_user_id'	=> $user_id,
			'status'		=> "<=1",
			);

		$sb		= new StatementBuilder( $config['config']['table_prefix'] );
		$bs		= new BugStatements( $sb );
		$bs->addComponent( 'Base' );
		$bs->addComponent( 'Developer', $user_id );
		$bs->addComponent( 'Status', array( 1, 3 ) );
		$query	= $sb->buildSelectStatement();
		$result	= $dbc->execute( $query );
		$count	= $result->recordCount();
	
		if( $output == "xml" )
		{
			$root	=& new XML_DOM_Node( 'bugs' );
			while( $entry	= $result->fetchNextArray( false ) )
			{
				$node	=& new XML_DOM_Node( 'bug' );
				$child	=& new XML_DOM_Node( 'title', $entry['title'] );
				$node->addChild( $child );
				$child	=& new XML_DOM_Node( 'server', $entry['server'] );
				$node->addChild( $child );
				$child	=& new XML_DOM_Node( 'project', $entry['project'] );
				$node->addChild( $child );
				$child	=& new XML_DOM_Node( 'status', $language->words['main']['states'][$entry['status']] );
				$node->addChild( $child );
				$child	=& new XML_DOM_Node( 'priority', $language->words['main']['priorities'][$entry['priority']] );
				$node->addChild( $child );
				$root->addChild( $node );
			}
			$xb	= new XML_DOM_Builder();
			header('Content-Type: text/xml');
			return $xb->build( $root );
		}
		else if( $output == "json" )
			return $this->toJSON( $data );
		else if( $output == "php" )
			return $this->toPHP( $data);
	}
}
?>